package com.company.netty.demo.NIO.ChannelDemos;

import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 使用ByteBuffer和FileChannel读取文件内容
 * */
public class FileChannelReadTest {

    public static void main(String[] args) throws Exception{

        //创建文件IO

        File file = new File("file.txt");

        FileInputStream fileInputStream = new FileInputStream(file);

        //获取channel，实际是FileChannelImpl, NIOFileChannel
        FileChannel fileChannel = fileInputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

       fileChannel.read(byteBuffer);

       byteBuffer.flip();

       System.out.println(new String(byteBuffer.array()));

       fileInputStream.close();

    }

}
